package cn.gxa.j229.bug.finance.controller.loanuser;

/*
 *
 *@author leijun
 *
 *@creating 2020/9/27 10:17
 *
 */

import cn.gxa.j229.bug.finance.BusinessException;
import cn.gxa.j229.bug.finance.pojo.Auth_Crud;
import cn.gxa.j229.bug.finance.pojo.Check;
import cn.gxa.j229.bug.finance.pojo.Permission;
import cn.gxa.j229.bug.finance.service.loanuser.Auth_CrudService;
import cn.gxa.j229.bug.finance.service.loanuser.CheckService;
import cn.gxa.j229.bug.finance.service.loanuser.impl.Auth_CrudServiceImpl;
import cn.gxa.j229.bug.finance.utils.*;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;
import sun.security.provider.MD5;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName CheckController
 * @Description TODO
 * @Author 俊
 * @Date 2020/9/27 10:17
 * @Version 1.0
 */
@Controller
@RequestMapping("/check")

public class CheckController {

    @Resource
    CheckService checkService;

    @Resource
    Auth_CrudService auth_crudService;


    @PostMapping("/reg")
    @ResponseBody
    public Map regist(@RequestBody Check check){
        JsonResult jsonResult = new JsonResult();
        int i = checkService.insertSelective(check);
        if (i==1){
            jsonResult.setCode("200");
            jsonResult.setMsg("注册成功");
            jsonResult.setData(i);
            return  jsonResult.getValues();
        }
        throw new BusinessException("500","注册失败，该用户已被注册");

    }

    @PostMapping("/login")
    @ResponseBody
    public Map login(@RequestBody Check check) throws BusinessException{
        JsonResult jsonResult = new JsonResult();
        List<Permission> i = checkService.login(check);
        Check user = checkService.findByUsername(check.getUsername());
        List<String> type = auth_crudService.getType(check.getUsername());
        if (i != null){
            JWTUtil jwtUtil = new JWTUtil();
            String token = jwtUtil.createJWT("1", check.getUsername(), 3600000,checkService.findTest(check));
            jsonResult.put("type",type);
            jsonResult.put("name",user.getName());
            jsonResult.put("token",token);
            jsonResult.put("menu",i);
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功");
            return jsonResult.getValues();
        }
        throw new BusinessException("500","登录失败，用户名或密码错误");
    }
    Integer authCode;
    Check user;
    @PostMapping("/authCode")
    @ResponseBody
    public Map authCode(@RequestBody Check check) throws MessagingException {
        JsonResult jsonResult = new JsonResult();

        user = checkService.findByEmail(check.getEmail());
        Check byUsername = checkService.findByUsername(check.getUsername());
        if (byUsername.getUsername().trim().equals(user.getUsername())){
            if (user!=null){
                authCode = SendEmail.email(user.getEmail());
                jsonResult.setCode("200");
                jsonResult.put("msg","验证码已经下发至您的邮箱，请在30分钟内输入验证码");
                return jsonResult.getValues();
            }
            throw new BusinessException("500","输入的邮箱错误");
        }
        throw new BusinessException("500","输入的用户名错误");
    }

    @PostMapping("/forget")
    @ResponseBody
    public Map forget(@RequestBody Check check){
        JsonResult jsonResult = new JsonResult();
        if (check.getAuthCode().trim().equals(authCode+"")){
            check.setSalt(user.getSalt());
            check.setUsername(user.getUsername());
            int i = checkService.updatePassword(check);
            if (i==1){
                jsonResult.setCode("200");
                jsonResult.put("msg","修改成功");
                return jsonResult.getValues();
            }
        }
        throw new BusinessException("500","修改失败");
    }
}
